<?php
/**
 * 商品模块
 */
namespace Home\Model; 
use Think\Model;
class ItemModel extends Model{
    /**
     * 商品列表
     */
    public function index($arg=array())
    {   
        $page=!empty($arg['page'])?intval($arg['page']):1;           
        //where 语句 
        $condition='';
        if(!empty($arg['typeid'])){
            $condition='typeid='.$arg['typeid'];
        }else{
            $condition.='1=1';
        }
        if(!empty($arg['brandid'])){
            $condition.=' AND brandid='.$arg['brandid'];
        }
        if(!empty($arg['keywords'])){
            $condition.=" AND itemname like '%".$arg['keywords']."%' or bn like '%".$arg['keywords']."%'";
        }
        //商家可以查看所在运营商+平台的产品
        $shopid=!empty($_SESSION['shopid'])?$_SESSION['shopid']:0;
        $yid=M('shop_user')->where('shopid=%d',$shopid)->getField('founder');
        $yid=intval($yid);

        if (!empty($yid))
        {
            $condition.= '  AND  platid !=0  OR  yid='.$yid;
        }
        
        $count=M()->query("select count(*) as count from ".C('DB_PREFIX').'item where '.$condition);
       
        $pagearr= getPageArr($page,C('PAGESIZE'),$count[0]['count']);
        $result=M('item')->where($condition)
        ->limit($pagearr['start'],$pagearr['pagesize'])
        ->order('itemid desc')->getField('itemid,itemname,bn,price,inventory, FROM_UNIXTIME(createtime, "%Y-%m-%d %H:%i:%S") createtime,supperprice,yid,examine,status');
        
        $temp['pagearr']=$pagearr;
        $temp['item']=!empty($result)?$result:'';
        //设置page cookie
        cookie('page',$page,120);
        return $temp;

    }


}
